/*
 * Copyright 2011 android-jar-fixer authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.perunlabs.tool.fixandroidjar;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * Method visitor that removes all code that follows call to super(...) or
 * this(...) in a constructor. Should not be used for normal methods.
 */
public class FixConstructorMethodVisitor implements MethodVisitor {
  private final MethodVisitor mv;
  private boolean constructorCodeReady;

  public FixConstructorMethodVisitor(MethodVisitor mv) {
    this.mv = mv;
    this.constructorCodeReady = false;
  }

  @Override
  public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
    if (constructorCodeReady) {
      return null;
    } else {
      return mv.visitAnnotation(desc, visible);
    }
  }

  @Override
  public AnnotationVisitor visitAnnotationDefault() {
    if (constructorCodeReady) {
      return null;
    } else {
      return mv.visitAnnotationDefault();
    }
  }

  @Override
  public void visitAttribute(Attribute attr) {
    if (!constructorCodeReady) {
      mv.visitAttribute(attr);
    }
  }

  @Override
  public void visitCode() {
    mv.visitCode();
  }

  @Override
  public void visitEnd() {
    mv.visitEnd();
  }

  @Override
  public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if (!constructorCodeReady) {
      mv.visitFieldInsn(opcode, owner, name, desc);
    }
  }

  @Override
  public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
    if (!constructorCodeReady) {
      mv.visitFrame(type, nLocal, local, nStack, stack);
    }
  }

  @Override
  public void visitIincInsn(int var, int increment) {
    if (!constructorCodeReady) {
      mv.visitIincInsn(var, increment);
    }
  }

  @Override
  public void visitInsn(int opcode) {
    if (!constructorCodeReady) {
      mv.visitInsn(opcode);
    }
  }

  @Override
  public void visitIntInsn(int opcode, int operand) {
    if (!constructorCodeReady) {
      mv.visitIntInsn(opcode, operand);
    }
  }

  @Override
  public void visitJumpInsn(int opcode, Label label) {
    if (!constructorCodeReady) {
      mv.visitJumpInsn(opcode, label);
    }
  }

  @Override
  public void visitLabel(Label label) {
    if (!constructorCodeReady) {
      mv.visitLabel(label);
    }
  }

  @Override
  public void visitLdcInsn(Object cst) {
    if (!constructorCodeReady) {
      mv.visitLdcInsn(cst);
    }
  }

  @Override
  public void visitLineNumber(int line, Label start) {
    if (!constructorCodeReady) {
      mv.visitLineNumber(line, start);
    }
  }

  @Override
  public void visitLocalVariable(String name, String desc, String signature, Label start,
      Label end, int index) {
    if (!constructorCodeReady) {
      mv.visitLocalVariable(name, desc, signature, start, end, index);
    }
  }

  @Override
  public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
    if (!constructorCodeReady) {
      mv.visitLookupSwitchInsn(dflt, keys, labels);
    }
  }

  @Override
  public void visitMaxs(int maxStack, int maxLocals) {
    if (!constructorCodeReady) {
      mv.visitMaxs(maxStack, maxLocals);
    }
  }

  @Override
  public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    if (!constructorCodeReady) {
      mv.visitMethodInsn(opcode, owner, name, desc);

      if (opcode == Opcodes.INVOKESPECIAL && name.equals("<init>")) {
        constructorCodeReady = true;
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(100, 100);
      }
    }
  }

  @Override
  public void visitMultiANewArrayInsn(String desc, int dims) {
    if (!constructorCodeReady) {
      mv.visitMultiANewArrayInsn(desc, dims);
    }
  }

  @Override
  public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
    if (constructorCodeReady) {
      return null;
    } else {
      return mv.visitParameterAnnotation(parameter, desc, visible);
    }
  }

  @Override
  public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
    if (!constructorCodeReady) {
      mv.visitTableSwitchInsn(min, max, dflt, labels);
    }
  }

  @Override
  public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
    if (!constructorCodeReady) {
      mv.visitTryCatchBlock(start, end, handler, type);
    }
  }

  @Override
  public void visitTypeInsn(int opcode, String type) {
    if (!constructorCodeReady) {
      mv.visitTypeInsn(opcode, type);
    }
  }

  @Override
  public void visitVarInsn(int opcode, int var) {
    if (!constructorCodeReady) {
      mv.visitVarInsn(opcode, var);
    }
  }
}
